package com.duqc.designpattern.responsibility;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName WordInterceptor
 * @Description SpringMVC的责任链模式(SpringMVC拦截器)
 * @Author duqc
 * @Date 2021/5/7
 **/
@Slf4j
public class WordInterceptor implements HandlerInterceptor {

    /**
     * 在执行controller定义的请求方法前执行
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        log.info("执行WordInterceptor的preHandle()");
        return true;
    }

    /**
     * controller定义的请求方法执行后，但还没渲染页面前，执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        log.info("执行WordInterceptor的postHandle()");
    }

    /**
     * 在渲染页面完毕后执行，或者preHandle()返回fasle时执行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        log.info("执行WordInterceptor的afterCompletion()");
    }
}
